package Email;

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;

import Entities.Candidate;

import java.text.SimpleDateFormat;
import java.util.*;

public class Email {

	private static final String PROTOCOL = "smtp";
	private static final String SMTP_HOST = "smtp.gmail.com";
	private static final String PORT = "465";
	private static final String SMTP_USERNAME = "noreply.online.voting.system";
	private static final String SMTP_PASSWORD = "online_1234";
	private static final String EMAIL = "noreply.online.voting.system@gmail.com";

	private Properties props;
	private Session mailSession;
	private Transport transport;
	private MimeMessage mimeMessage;

	public Email() {
		_setProperties();
		mailSession = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(SMTP_USERNAME,
								SMTP_PASSWORD);
					}
				});
		mailSession.setDebug(false);
		try {
			transport = mailSession.getTransport(PROTOCOL);
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}
		
		mimeMessage = new MimeMessage(mailSession);
		try {
			mimeMessage.setFrom(new InternetAddress(EMAIL));
		} catch (AddressException e) {
			System.out.println("Exception: Couldn't send the message.");
		} catch (MessagingException e) {
			System.out.println("Exception: Couldn't send the message.");
		}
	}
	
	public void postMail(String recipient, String subject, String message,
			boolean isHtml) throws MessagingException {
		postMail(new String[] { recipient }, subject, message, isHtml);
	}

	public void postMail(String recipients[], String subject, String message,
			boolean isHtml) throws MessagingException {
		mimeMessage.setSubject(subject);
	//	if (isHtml)
			mimeMessage.setContent(message, "text/html; charset=ISO-8859-1");
		InternetAddress[] toInternetAddress = _getInternetAddressFromStringArray(recipients);
		mimeMessage.setRecipients(RecipientType.TO, toInternetAddress);
		_send();
	}

	private void _setProperties() {
		props = new Properties();
		props.setProperty("mail.transport.protocol", PROTOCOL);
		props.setProperty("mail.host", SMTP_HOST);
		props.put("mail.smtp.port", PORT);
		props.put("mail.smtp.auth", "true");
		props.put("mail.user", SMTP_USERNAME);
		props.put("mail.password", SMTP_PASSWORD);
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");

	}

	private InternetAddress[] _getInternetAddressFromStringArray(
			String[] recipients) throws AddressException {
		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		return addressTo;
	}

	private boolean _send() throws MessagingException {
		transport.connect();
		transport.sendMessage(mimeMessage, mimeMessage
				.getRecipients(RecipientType.TO));
		transport.close();
		return true;
	}
	
	public void sendRegPassword(String id,String email,String password)
	{
		String to = email;
		String subject = "Voting System Registration";
		//Registration
		String messageBody = "<div>Welcome "+id + " " + email.split("@")[0] +" to our voting system<br>In order to complete registration log in using this password :- "+ password +"</div>";
		boolean isHtml = true;
		try {
			this.postMail(to, subject, messageBody, isHtml);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendVoting(String email)
	{
		Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		String to = email;
		String subject = "Voting System Vote Status";
		//Registration
		String messageBody = "<div>You have voted successfully at "+ dateFormat.format(calendar.getTime())+"</div>";
		boolean isHtml = true;
		try {
			this.postMail(to, subject, messageBody, isHtml);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Date start = new Date();
		System.out.println(start);
		Email m = new Email();
		String to = "elshahed54@hotmail.com";
		String subject = "Voting System";
		String messageBody = "<h1>Heading Test</h1> <div>Hi :D</div>";
		boolean isHtml = true;
		try {
			m.postMail(to, subject, messageBody, isHtml);

			Date end = new Date();
			System.out.println(end);
			Long time = (end.getTime() - start.getTime()) / 1000l;
			if (time == 1)
				System.out.println("Message sent in about " + time + " sec!");
			else
				System.out.println("Message sent in about " + time + " secs!");
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}